/**
 * Created by zhouli on 18/2/7
 * Email li.zhou@huilianyi.com
 */
import React from 'react';
import {fitText, messages} from 'share/common';
import {Tooltip, Select} from 'antd';

const Option = Select.Option;
import moment from 'moment';

export default {
  //检测岗位
  checkCompanyMainPosition(list) {
    //必须要有一个主岗位
    let len = list.length;
    for (let i = 0; i < len; i++) {
      if (list[i].companyMainPosition) {
        return true
      }
    }
  },
  //要更新的时候人员实体
  getUpdatePersonObj(personObj, values) {
    let res = Object.assign({}, {
      userOID: personObj.userOID,
      directManager: values.directManager[0] ? values.directManager[0].userOID : "",
      employeeTypeCode: values.employeeType[0] ? values.employeeType[0].value : null,
      email: values.email,
      mobile: values.mobile,
      countryCode: values.mobilePrefix.split("$$").length > 1 ? values.mobilePrefix.split("$$")[0] : personObj.countryCode,
      mobileCode: values.mobilePrefix.split("$$").length > 1 ? values.mobilePrefix.split("$$")[1] : personObj.mobileCode,//手机前缀:默认86
      employeeID: values.employeeID,
      fullName: values.fullName,
      //男
      // gender: values.gender,//必须是int类型不然报错,可以不传，后端用的是 genderCode
      genderCode: values.gender,
      birthday: values.birthday ? values.birthday.format("YYYY-MM-DD") : null,
      entryTime: values.entryTime ? values.entryTime.format("YYYY-MM-DD") : null,
      businessCardStatus: values.businessCardStatus ? values.businessCardStatus : null,
    });
    return res;
  },
  getCreatePersonObj(personObj, personObjDefaultWithoutExtend, values) {
    return Object.assign(personObjDefaultWithoutExtend, {
      directManager: values.directManager[0] ? values.directManager[0].userOID : "",
      employeeTypeCode: values.employeeType[0] ? values.employeeType[0].value : personObj.employeeTypeCode,
      email: values.email,
      mobile: values.mobile,
      countryCode: values.mobilePrefix.split("$$").length > 1 ? values.mobilePrefix.split("$$")[0] : personObj.countryCode,
      mobileCode: values.mobilePrefix.split("$$").length > 1 ? values.mobilePrefix.split("$$")[1] : personObj.mobileCode,//手机前缀:默认86
      employeeID: values.employeeID,
      fullName: values.fullName,
      // gender: values.gender,//必须是int类型不然报错，可以不传，后端用的是genderCode
      genderCode: values.gender,
      birthday: values.birthday ? values.birthday.format("YYYY-MM-DD") : null,
      entryTime: values.entryTime ? values.entryTime.format("YYYY-MM-DD") : null,
      businessCardStatus: values.businessCardStatus ? values.businessCardStatus : null,
    });
  },
  renderNoEditingTextTime(date) {
    if (date) {
      return moment(date).format("YYYY-MM-DD");
    } else {
      return "";
    }
  },
  renderToBusinessCardStatusText(data){
    if (data === 1){
      return messages('enterprise-manage.key497'/*申请中（政策要求）*/);
    }else if (data === 2){
      return messages('enterprise-manage.key498'/*申请中（个人要求）*/);
    }else if (data === 3){
      return messages('enterprise-manage.key499'/*待申请*/);
    }else if (data === 4){
      return messages('enterprise-manage.key500'/*申请被拒绝*/);
    }else if (data === 5){
      return messages('enterprise-manage.key501'/*使用中*/);
    }else if (data === 6){
      return messages('enterprise-manage.key502'/*未开卡*/);
    }else if (data === 7){
      return messages('enterprise-manage.key503'/*注销*/);
    }
  },
  //渲染字段的内容，根据情况进行截取，鼠标浮动有提示
  renderNoEditingText(text) {
    let _text = fitText(text, 12);
    if (_text.text) {
      return (
        <Tooltip title={_text.origin}>
          <span>{_text.text}</span>
        </Tooltip>
      )
    } else {
      return (text)
    }
  },
  getShowItems(person) {
    return [
      {
        label: messages('enterprise-manage.key230'/*工号*/) + ":",
        name: this.renderNoEditingText(person.employeeID)
      },
      {
        label: messages('enterprise-manage.key231'/*姓名*/) + ":",
        name: this.renderNoEditingText(person.fullName)
      },
      {
        label: messages('enterprise-manage.key234'/*邮箱*/) + ":",
        name: this.renderNoEditingText(person.email)
      },
      {
        label: messages('enterprise-manage.key235'/*手机*/) + ":",
        name: this.renderNoEditingText(person.mobile)
      },
      {
        label: messages('enterprise-manage.key236'/*直属领导*/) + ":",
        name: this.renderNoEditingText(person.directManagerName)
      },
      {
        label: messages('enterprise-manage.key239'/*人员类型*/) + ":",
        name: this.renderNoEditingText(person.employeeType)
      },
      {
        label: messages('enterprise-manage.key241'/*性别*/) + ":",
        name: person.gender
      },
      {
        label: messages('enterprise-manage.key242'/*生日*/) + ":",
        name: this.renderNoEditingTextTime(person.birthday)
      },
      {
        label: messages('enterprise-manage.key243'/*入职时间*/) + ":",
        name: this.renderNoEditingTextTime(person.entryTime)
      },
      {
        label: messages('enterprise-manage.key496'/*公务卡状态*/) + ":",
        name: this.renderToBusinessCardStatusText(person.businessCardStatus)
      },
    ]
  },
  renderMobilePrefix(preFixList) {
    if (preFixList.length > 1) {
      let preFixListDom = preFixList.map((item) => {
        return (
          // CN + 86 + 中国
          <Option value={item.shortName + "$$" + item.countryCode + "$$" + item.countryName}
                  key={item.countryCode}>{item.countryName + "(" + item.countryCode + ")"}</Option>
        )
      })
      return (
        <Select style={{width: 150}} showSearch>
          {preFixListDom}
        </Select>
      )
    } else {
      return (
        <Select style={{width: 150}}>
          <Option value="86">+86</Option>
        </Select>
      )
    }
  },
  // -----扩展字段-非编辑状态---start
  renderContentByMessageKey(field) {
    let messageKey = field.messageKey;
    //分为：单行输入框，多行输入框，值列表，日期，数字，时间，图片
    switch (messageKey) {
      case "input": {
        return this.renderFiled_input(field);
        break;
      }
      //话费额度
      case "MonthLimited": {
        return this.renderFiled_input(field);
        break;
      }
      //用车制度
      case "regulation_input": {
        return this.renderFiled_input(field);
        break;
      }
      case "text_area": {
        return this.renderFiled_text_area(field);
        break;
      }
      case "cust_list": {
        return this.renderFiled_cust_list(field);
        break;
      }
      case "date": {
        return this.renderFiled_date(field);
        break;
      }
      case "common.date": {
        return this.renderFiled_date(field);
        break;
      }
      case "number": {
        return this.renderFiled_number(field);
        break;
      }
      case "time": {
        return this.renderFiled_time(field);
        break;
      }
      case "image": {
        return this.renderFiled_image(field);
        break;
      }
    }
  },
  renderFiled_input(field) {
    let messageKey = field.messageKey;
    //后端可能返回的是值列表值对应的code（value），不是messageKey，需要找一下
    //参见bug13014
    if (messageKey === "cust_list" &&
      field.customEnumerationList &&
      field.customEnumerationList.values &&
      field.customEnumerationList.values.length &&
      field.customEnumerationList.values.length > 0) {
      let customEnumerationList = field.customEnumerationList.values;
      for (let i = 0; i < customEnumerationList.length; i++) {
        if (field.value === customEnumerationList[i].value) {
          field.value = customEnumerationList[i].messageKey;
        }
      }
    }
    return (
      <div className="info-item-extend " key={field.fieldOID}>
        <div className="info-item-title">{field.fieldName}：</div>
        <div className="info-item-text">
          {this.renderNoEditingText(field.value)}
        </div>
      </div>
    )
  },
  renderFiled_text_area(field) {
    return this.renderFiled_input(field);
  },
  renderFiled_cust_list(field) {
    return this.renderFiled_input(field);
  },
  renderFiled_date(field) {
    let date = field.value ? (new Date(field.value)).format("yyyy-MM-dd") : "";
    return (
      <div className="info-item-extend " key={field.fieldOID}>
        <div className="info-item-title">{field.fieldName}：</div>
        <div className="info-item-text">{date}</div>
      </div>
    )
  },
  renderFiled_number(field) {
    field.unit = "";
    if (field.fieldContent) {
      let content = {};
      try {
        content = JSON.parse(field.fieldContent);
      } catch (e) {
      }
      field.unit = content.unit;
    }
    return (
      <div className="info-item-extend " key={field.fieldOID}>
        <div className="info-item-title">{field.fieldName}：</div>
        <div className="info-item-text">{field.value}&nbsp;{field.value ? field.unit : ""}</div>
      </div>
    )
  },
  renderFiled_time(field) {
    let val = moment(field.value, 'HH:mm').format("HH:mm");
    if (field.value === null || field.value === "") {
      val = "";
    }
    if (field.value && field.value.length > 5) {
      //兼容以前的
      val = moment(field.value).format("HH:mm");
    }
    return (
      <div className="info-item-extend " key={field.fieldOID}>
        <div className="info-item-title">{field.fieldName}：</div>
        <div className="info-item-text">{val}</div>
      </div>
    )
  },
  renderFiled_image(field) {
    //可能需要渲染多张图片（1-3张）
    function _getTooltipImages(imgs) {
      let imgsDom = [];
      if (imgs && imgs.length > 0) {
        imgs.map((item, index) => {
          imgsDom.push(
            <Tooltip key={index} title={
              <img className="info-item-img-tip"
                   src={item.thumbnailUrl}/>
            }>
              <img src={item.thumbnailUrl}/>
            </Tooltip>
          )
        })
        return imgsDom;
      } else {
        return (<div/>);
      }
    }

    return (
      <div className="info-item-extend " key={field.fieldOID}>
        <div className="info-item-title">{field.fieldName}：</div>
        <div className="info-item-text">
          {_getTooltipImages(field.attachmentImages)}
        </div>
      </div>
    )
  },
  // -----扩展字段-非编辑状态---end

}
